package com.gmzg.gmzgoffice.report;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.gmzg.gmzgoffice.R;
import com.gmzg.gmzgoffice.entity.Weekly_report;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.UpdateListener;

/**
 * A simple {@link Fragment} subclass.
 */
public class ReportFragment extends Fragment implements ReportAdapter.onChildClickListener, ReportAdapter.onChildLongClickListener {

    private Toolbar mToolbar;
    private ReportAdapter mAdapter;
    private RecyclerView mRecyclerView;
    private List<Weekly_report> mReportsList;
    private static final String TAG = "ReportFragment";
    private int clickPosition = 999999;
    public ReportFragment() {
        // Required empty public constructor
    }

    /**
     * !!! try to load and reload date in this method
     * this life cycle will execute after onCreateView() in the very first time
     */
    @Override
    public void onResume() {
        super.onResume();
        queryDate();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_report, container, false);
        initView(view);
        recycleFun();
        return view;
    }

    private void recycleFun() {
        RecyclerView.LayoutManager manager = new LinearLayoutManager(
                getContext(),
                LinearLayoutManager.VERTICAL,
                false
        );
        mRecyclerView.setLayoutManager(manager);
        mReportsList = new ArrayList<>();
        mAdapter = new ReportAdapter(getContext(),mReportsList);
        mAdapter.onChildChlickListener(this);
        mAdapter.onChildLongClickListener(this);
        mRecyclerView.setAdapter(mAdapter);
    }


    private void initView(View itemView){
        Bmob.initialize(getContext(),"18339e37fbd7c177d16f1204ccc849a3");
        mToolbar = (Toolbar) itemView.findViewById(R.id.report_fragment_tool_bar);
        mRecyclerView = ((RecyclerView) itemView.findViewById(R.id.report_fragment_recylcer_view));
        mToolbar.setTitle("");
        setHasOptionsMenu(true);
        ((AppCompatActivity) getActivity()).setSupportActionBar(mToolbar);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.report_menu,menu);
    }

    //response options menu click
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Log.d(TAG, "onOptionsItemSelected: 点击了菜单");
        int itemId = item.getItemId();
        switch (itemId) {
            case R.id.report_menu:
                Intent intent = new Intent(getContext(), ReportActivity.class);
                startActivity(intent);
                break;
        }
        return true;
    }

    // query the form from Bmob
    private void queryDate() {
        Log.d(TAG, "queryDate: queryTextMethod");
        BmobQuery<Weekly_report> bmobQuery = new BmobQuery<>();
        bmobQuery.findObjects(new FindListener<Weekly_report>() {
            @Override
            public void done(List<Weekly_report> list, BmobException e) {
                if (e == null) {
                    mAdapter.removeAllData();
                    mAdapter.addAll(list);
                }
            }
        });
    }

    // response the click event
    @Override
    public void onChildclickListener(RecyclerView parent, View view, int position) {
//        Toast.makeText(getContext(), position + "", Toast.LENGTH_SHORT).show();
        clickPosition = position;
        BmobQuery<Weekly_report> bmobQuery = new BmobQuery<>();
        bmobQuery.findObjects(new FindListener<Weekly_report>() {
            @Override
            public void done(List<Weekly_report> list, BmobException e) {
                if (e == null) {
                    if (clickPosition != 999999){
                        String finishWork = list.get(clickPosition).getFinish_work();
                        String workSummary = list.get(clickPosition).getWork_summary();
                        String nextPlan = list.get(clickPosition).getNext_plan();
                        String needHelp = list.get(clickPosition).getNeed_help();
                        String notes = list.get(clickPosition).getNotes();
                        Intent intent = new Intent(getContext(), ReportDetailActivity.class);
                        intent.putExtra("finishWork", finishWork);
                        intent.putExtra("workSummary", workSummary);
                        intent.putExtra("nextPlan", nextPlan);
                        intent.putExtra("needHelp", needHelp);
                        intent.putExtra("notes",notes);
                        startActivity(intent);
                    }
                }
            }
        });
    }

    @Override
    public void onChildLongClickListener(final int position) {
        BmobQuery<Weekly_report> bmobQuery = new BmobQuery<>();
        bmobQuery.findObjects(new FindListener<Weekly_report>() {
            @Override
            public void done(List<Weekly_report> list, BmobException e) {
//                String objectId = list.get(clickPosition).getObjectId();
                Weekly_report report = list.get(position);
                report.delete(new UpdateListener() {
                    @Override
                    public void done(BmobException e) {
                        if (e == null) {
                            Toast.makeText(getContext(), "删除成功", Toast.LENGTH_SHORT).show();
                            queryDate();
                        } else {
                            Toast.makeText(getContext(), "删除失败", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        });
    }
}
